package com.dong.rocketmq_demo.controller;

import com.dong.rocketmq_demo.bean.User;
import com.dong.rocketmq_demo.producer.Producer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class RocketController {
    private final String topic = "TestTopic";
    @Resource
    private Producer producer;

    @RequestMapping("/sendMessage")
    public String sendMessage(String message) {
        producer.sendMessage(topic, message);
        return "消息发送完成";
    }

    //这个发送事务消息的例子中有很多问题，需要注意下。
    @RequestMapping("/sendTransactionMessage")
    public String sendTransactionMessage(String message) throws InterruptedException {
        producer.sendMessageInTransaction(topic, message);
        return "消息发送完成";
    }

    //这个发送事务消息的例子中有很多问题，需要注意下。
    @RequestMapping("/sendMessageOrderLy")
    public String sendMessageOrderLy() throws InterruptedException {
        List<User> list = new ArrayList<>();
        User user1 = new User("创建", 1);
        User user2 = new User("创建", 2);
        User user5 = new User("中间", 1);
        User user6 = new User("中间", 2);
        User user7 = new User("中间", 3);
        User user8 = new User("中间", 4);
        User user9 = new User("完成", 1);
        User user10 = new User("完成", 2);
        User user11 = new User("完成", 3);
        User user12 = new User("完成", 4);
        User user3 = new User("创建", 3);
        User user4 = new User("创建", 4);
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        list.add(user6);
        list.add(user7);
        list.add(user8);
        list.add(user9);
        list.add(user10);
        list.add(user11);
        list.add(user12);
        producer.sendMessageOrderly(topic, list);
        return "消息发送完成";
    }


}
